package com.wl.jetpackdemo.bottomtab

import android.util.SparseArray
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter

/**
 *
 * Created by wangliang on 2021/1/30.
 */
class AdapterFragmentPager(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {
    private val fragments: SparseArray<BaseFragment> = SparseArray()
    init {
        fragments.put(PAGE_INDEX, IndexFragment.getInstance())
        fragments.put(PAGE_PARTY, PartyFragment.getInstance())
        fragments.put(PAGE_MESSAGE, MessageFragment.getInstance())
        fragments.put(PAGE_MINE, MineFragment.getInstance())
    }

    companion object {
        const val PAGE_INDEX = 0
        const val PAGE_PARTY = 1
        const val PAGE_MESSAGE = 2
        const val PAGE_MINE = 3
    }

    override fun getItemCount(): Int {
        return fragments.size()
    }

    override fun createFragment(position: Int): Fragment {
        return fragments[position]
    }
}